package cn.wikiflyer.beikang.act.user;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.user.bean.HelpBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wikiflyer.beikang.utils.BKUtils;
import cn.wk.libs4a.net.WKAsyncTaskPro;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.umeng.socialize.utils.Log;

/**
 * 了解倍康
 * 
 * @author qunlee_mr
 * 
 */
public class UserKnowBeikangAct extends BaseActivity {

	private String catId = "13";
	private TextView tv_title;
	private TextView tv_content;

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_user_knowbeikang);
		initSize();
	}

	@Override
	public void init() {
		tv_title = (TextView) findViewById(R.id.textView_title);
		tv_content = (TextView) findViewById(R.id.textView_content);
		findViewById(R.id.textView1).setOnClickListener(
				new OnTabClickListener());
		findViewById(R.id.textView2).setOnClickListener(
				new OnTabClickListener());
		findViewById(R.id.textView3).setOnClickListener(
				new OnTabClickListener());
		findViewById(R.id.textView4).setOnClickListener(
				new OnTabClickListener());
		findViewById(R.id.textView5).setOnClickListener(
				new OnTabClickListener());
	}

	@Override
	public void setData() {
		new GetHelpTask(ctx);
	}

	/**
	 * 顶部按钮点击切换，监听事件
	 */
	class OnTabClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			updateBtn(v.getId());
			switch (v.getId()) {
			case R.id.textView1:
				catId = "13";
				break;
			case R.id.textView2:
				catId = "14";
				break;
			case R.id.textView3:
				catId = "15";
				break;
			case R.id.textView4:
				catId = "16";
				break;
			case R.id.textView5:
				catId = "23";
				break;
			default:
				break;
			}
			new GetHelpTask(ctx);
		}
	}

	private void updateBtn(int id) {
		findViewById(R.id.textView1).setBackgroundColor(
				getResources().getColor(R.color.white));
		findViewById(R.id.textView2).setBackgroundColor(
				getResources().getColor(R.color.white));
		findViewById(R.id.textView3).setBackgroundColor(
				getResources().getColor(R.color.white));
		findViewById(R.id.textView4).setBackgroundColor(
				getResources().getColor(R.color.white));
		findViewById(R.id.textView5).setBackgroundColor(
				getResources().getColor(R.color.white));

		findViewById(id).setBackgroundColor(
				getResources().getColor(R.color.bk_about_click));

	}

	/**
	 * 更新界面数据
	 */
	private void updateUIData(final HelpBean bean) {
		tv_title.setText(bean.title);
		//
		tv_content.setMovementMethod(ScrollingMovementMethod.getInstance()); // 设置滚动
		final MyHandler myHandler = new MyHandler(this);
		Thread t = new Thread(new Runnable() {
			Message msg = Message.obtain();

			@Override
			public void run() {
				/**
				 * 要实现图片的显示需要使用Html.fromHtml的一个重构方法：public static Spanned
				 * fromHtml (String source, Html.ImageGetterimageGetter,
				 * Html.TagHandler
				 * tagHandler)其中Html.ImageGetter是一个接口，我们要实现此接口，在它的getDrawable
				 * (String source)方法中返回图片的Drawable对象才可以。
				 */
				ImageGetter imageGetter = new ImageGetter() {

					@Override
					public Drawable getDrawable(String source) {
						URL url;
						Drawable drawable = null;
						try {
							url = new URL(source);
							drawable = Drawable.createFromStream(
									url.openStream(), null);
							int paddingValue = BKUtils.dip2px(ctx, 10);
							Log.e("wj", "padding :" + paddingValue);
							drawable.setBounds(
									paddingValue,
									0,
									width - 2 * paddingValue,
									drawable.getIntrinsicHeight()
											* (width / drawable
													.getIntrinsicWidth()));
						} catch (MalformedURLException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
						return drawable;
					}
				};
				CharSequence test = Html.fromHtml(bean.content, imageGetter,
						null);
				msg.what = 0x101;
				msg.obj = test;
				myHandler.sendMessage(msg);
			}
		});
		t.start();
	}

	/*
	 * Handler
	 * 类应该应该为static类型，否则有可能造成泄露。在程序消息队列中排队的消息保持了对目标Handler类的应用。如果Handler是个内部类，那
	 * 么它也会保持它所在的外部类的引用。为了避免泄露这个外部类，应该将Handler声明为static嵌套类，并且使用对外部类的弱应用。
	 */
	private static class MyHandler extends Handler {
		WeakReference<UserKnowBeikangAct> mActivity;

		public MyHandler(UserKnowBeikangAct activity) {
			mActivity = new WeakReference<UserKnowBeikangAct>(activity);
		}

		@Override
		public void handleMessage(Message msg) {
			UserKnowBeikangAct theActivity = mActivity.get();
			if (msg.what == 0x101) {
				theActivity.tv_content.setText((CharSequence) msg.obj);
			}
			super.handleMessage(msg);
		}
	}

	/**
	 * 获取帮助接口（了解倍康）
	 */
	class GetHelpTask extends WKAsyncTaskPro {
		public GetHelpTask(Context context) {
			super(context, true);
		}

		@Override
		protected String doInBackground(boolean... isLocal) {
			return app().net.getHelp(catId, isLocal);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean<HelpBean>>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean<HelpBean> base = gson.fromJson(result, cvbType);
			if (base.status == 1) {
				updateUIData(base.data);
			} else {
			}
		}
	}

}
